package com.qyhl.module_activities.act.player.list;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.tools.ScreenUtils;
import com.qyhl.module_activities.R;
import com.qyhl.module_activities.common.ActivityConstant;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.act.PlayerVOBean;
import com.qyhl.webtv.commonlib.service.UserService;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;
import java.util.Random;

import es.dmoral.toasty.Toasty;

public class PlayerCommonAdapter extends CommonAdapter<PlayerVOBean> {
    private Integer mPlayerId = 0;
    private int actId;
    private PlayerListPresenter mPresenter;
    private Activity activity;
    private int voteStatus;
    private int actStatus;
    private int imgW;

    //投票标志
    private boolean canVote = true;

    public void recoveryCanVote() {
        canVote = true;
    }

    public PlayerCommonAdapter(Activity activity, int layoutId, List<PlayerVOBean> datas, int actId, PlayerListPresenter mPresenter, int voteStatus, int actStatus) {
        super(activity, layoutId, datas);
        this.actId = actId;
        this.activity = activity;
        this.voteStatus = voteStatus;
        this.actStatus = actStatus;
        this.mPresenter = mPresenter;
        imgW = (ScreenUtils.getScreenWidth(activity) - ScreenUtils.dip2px(activity, 20)) / 2;
    }

    @Override
    protected void convert(ViewHolder holder, PlayerVOBean playerVOBean, int i) {
        //头像
        ImageView image = holder.getView(R.id.player_avatar);
        //用户昵称
        TextView name = holder.getView(R.id.player_name);
        //票数
        TextView votes = holder.getView(R.id.player_votes);
        Button vote = holder.getView(R.id.player_vote);
        vote.setTag(playerVOBean);

        ViewGroup.LayoutParams params = image.getLayoutParams();
        params.height = getRandomHeight(playerVOBean);
        Glide.with(mContext)
                .load(playerVOBean.getPlayer().getApplyUrl())
                .apply(new RequestOptions().error(R.drawable.cover_large_default))
                .into(image);
        name.setText(playerVOBean.getPlayer().getPlayerName());
        votes.setText(String.valueOf(playerVOBean.getVoteNumber()));
        if (actStatus == ActivityConstant.STATUS_END || voteStatus == ActivityConstant.VOTE_END) {
            vote.setText("投票结束");
            vote.setEnabled(false);
        }else {
            vote.setText("为Ta投票");
            vote.setEnabled(true);
        }
        vote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (canVote) {
                    canVote = false;
                    CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                        @Override
                        public void login(boolean isLogin) {
                            String userName = CommonUtils.getInstance().getUserName();
                            if (isLogin) {
                                if (actStatus == ActivityConstant.STATUS_PROCESSING) {
                                    if (voteStatus == ActivityConstant.VOTE_ON) {
                                        PlayerVOBean player = (PlayerVOBean) v.getTag();
                                        mPlayerId = player.getPlayer().getId();
                                        mPresenter.vote(actId, userName, mPlayerId);
                                    } else if (voteStatus == ActivityConstant.VOTE_OFF) {
                                        mPresenter.onError(2, "投票未开始");
                                    } else if (voteStatus == ActivityConstant.VOTE_END) {
                                        mPresenter.onError(2, "投票已结束");
                                    }
                                } else if (actStatus == ActivityConstant.STATUS_END) {
                                    mPresenter.onError(2, "投票已结束");
                                }
                            } else {
                                Toasty.normal(activity, "尚未登录或登录已失效").show();
                                RouterManager.routerLogin(activity, 0);
                                canVote = true;
                            }
                        }

                        @Override
                        public void error(String s) {
                            canVote = true;
                        }
                    });

                }
            }
        });
    }

    private int getRandomHeight(PlayerVOBean playerVOBean) {//得到item的高度
        int r;
        if (playerVOBean.getApplyCoverWidth() != 0 && playerVOBean.getApplyCoverHeight() != 0) {
            float ratio = (float) playerVOBean.getApplyCoverWidth() / imgW;
            r = (int) (playerVOBean.getApplyCoverHeight() / ratio);
        } else {
            Random random = new Random();
            r = 350 + random.nextInt(200);
        }
        return r;
    }

    public Integer getMPlayerId() {
        return mPlayerId;
    }
}
